package com.czl.module_storehouse.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.module_base.fragment.BaseFragment;
import com.czl.module_base.http.HttpResponse;
import com.czl.module_base.mvp.presenter.InjectPresenter;
import com.czl.module_base.mvp.view.SimpleView;
import com.czl.module_base.utils.GlideUtils;
import com.czl.module_service.event.SortBeanEvent;
import com.czl.module_service.event.StorehouseHomeScanBackEvent;
import com.czl.module_service.event.StorehouseHomeScanEvent;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.activity.allocate.add.AllocateAddActivity;
import com.czl.module_storehouse.activity.borrow.add.BorrowAddActivity;
import com.czl.module_storehouse.activity.receive.add.ReceiveAddActivity;
import com.czl.module_storehouse.activity.shift.add.ShiftAddActivity;
import com.czl.module_storehouse.databinding.FragmentGoodsBaseInfoBinding;
import com.czl.module_storehouse.mvp.presenter.StockPresenter;
import com.jeremyliao.liveeventbus.LiveEventBus;

/**
 * @author by xubin
 * PS: Not easy to write code, please indicate.
 */
public class GoodsBaseInfoFragment extends BaseFragment<FragmentGoodsBaseInfoBinding> implements SimpleView {

    @InjectPresenter
    StockPresenter mStockPresenter;
    private SortBean mSortBean;
    private String mSortCode;
    private Integer mSortId;

    @Override
    protected FragmentGoodsBaseInfoBinding getBinding(LayoutInflater layoutInflater, ViewGroup container) {
        return FragmentGoodsBaseInfoBinding.inflate(layoutInflater, container, false);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        LiveEventBus.get(SortBeanEvent.class).observe(this, event -> loadData(event.getSort()));
        binding.textReceive.setOnClickListener(v -> startReceiveActivity());
        binding.textBorrow.setOnClickListener(v -> startBorrowActivity());
        binding.textAllocate.setOnClickListener(v -> startAllocateActivity());
        binding.textShift.setOnClickListener(v -> startShiftActivity());
        LiveEventBus.get(StorehouseHomeScanBackEvent.class).observe(this, storehouseHomeScanBackEvent -> requireActivity().finish());
    }

    private void startReceiveActivity() {
        SortBean sortBean = createProduct();
        if (sortBean != null) {
            startActivity(new Intent(getContext(), ReceiveAddActivity.class));
        }
    }

    private void startBorrowActivity() {
        if (createProduct() != null) {
            startActivity(new Intent(getContext(), BorrowAddActivity.class));
        }
    }

    private void startAllocateActivity() {
        if (createProduct() != null) {
            startActivity(new Intent(getContext(), AllocateAddActivity.class));
        }
    }

    private void startShiftActivity() {
        if (createProduct() != null) {
            startActivity(new Intent(getContext(), ShiftAddActivity.class));
        }
    }

    private SortBean createProduct() {
        if (mSortBean == null) {
            return null;
        }
        mSortBean.setSelected(true);
        mSortBean.setLocatList(null);
        LiveEventBus.get(StorehouseHomeScanEvent.class).postDelay(new StorehouseHomeScanEvent(mSortBean), 200);
        return mSortBean;
    }

    @Override
    protected void reload() {
        if (!TextUtils.isEmpty(mSortCode)) {
            loadData(null, mSortCode);
        } else if (mSortId != null && mSortId > 0) {
            loadData(mSortId, null);
        }
    }

    private void loadData(SortBean sortBean) {
        loadData(sortBean.getSortId(), null);
    }

    public void loadData(Integer sortId, String sortCode) {
        mSortId = sortId;
        mSortCode = sortCode;
        if (mStockPresenter != null) {
            mStockPresenter.getStorehouseSortStockInfo(sortId, sortCode);
        }
    }

    private void setupView(SortBean sortBean) {
        if (sortBean != null) {
            binding.tvTitle.setText(sortBean.getSortName());
            binding.tvCode.setText(sortBean.getSortCode());
            binding.tvCompany.setText(sortBean.getPropCompanyShortName());
            binding.tvSort.setText(sortBean.getClassificationName());
            binding.tvModel.setText(sortBean.getSortModel());
            binding.tvUnit.setText(sortBean.getUnit());
            binding.tvManageMode.setText(sortBean.getManageModeStr());
            binding.tvUseMode.setText(sortBean.getUseModeStr());
            binding.tvValuationMode.setText(sortBean.getValuationStr());
            int num = (sortBean.getWarnNum() == null ? 0 : sortBean.getWarnNum()) - sortBean.stockNum();
            if (num > 0) {
                binding.tvLowNum.setText(String.valueOf(num));
            } else {
                binding.tvLowNum.setText("无");
            }
            GlideUtils.loadImage(getContext(), binding.ivSort, sortBean.getSortImg(), R.mipmap.pic_default_loading, R.mipmap.pic_default_error);
        }
    }

    @Override
    public <T> void showHttpResponse(HttpResponse<T> httpResponse) {
        super.showHttpResponse(httpResponse);
        mSortBean = (SortBean) httpResponse.getData();
        setupView(mSortBean);
    }
}
